package com.chixing.service.impl;

import com.chixing.entity.CommentDoc;
import com.chixing.repository.CommentDocRepository;
import com.chixing.service.ICommentDocService;
import com.chixing.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

@Service
public class CommentDocServiceImpl implements ICommentDocService {

    @Autowired
    private CommentDocRepository commentDocRepository;

    @Override
    public Result postComment(CommentDoc commentDoc) {
        try {
            commentDoc.setCreateTime(LocalDateTime.now());
            commentDoc.setUpdateTime(LocalDateTime.now());
            commentDoc.setParentId(null); // 一级评论parentId为null

            CommentDoc savedComment = commentDocRepository.save(commentDoc);
            return Result.saveSuccess(savedComment);
        } catch (Exception e) {
            return Result.error("评论发布失败：" + e.getMessage());
        }
    }

    @Override
    public Result replyComment(String parentId, CommentDoc commentDoc) {
        try {
            // 检查父评论是否存在
            Optional<CommentDoc> parentCommentOpt = commentDocRepository.findById(parentId);
            if (!parentCommentOpt.isPresent()) {
                return Result.error("父评论不存在");
            }

            CommentDoc parentComment = parentCommentOpt.get();

            // 设置评论属性
            commentDoc.setMovieId(parentComment.getMovieId());
            commentDoc.setParentId(parentId);
            commentDoc.setCreateTime(LocalDateTime.now());
            commentDoc.setUpdateTime(LocalDateTime.now());

            // 保存评论
            CommentDoc savedComment = commentDocRepository.save(commentDoc);
            return Result.saveSuccess(savedComment);
        } catch (Exception e) {
            return Result.error("回复失败：" + e.getMessage());
        }
    }

    @Override
    public Result likeComment(String commentId) {
        try {
            Optional<CommentDoc> commentOpt = commentDocRepository.findById(commentId);
            if (!commentOpt.isPresent()) {
                return Result.error("评论不存在");
            }

            CommentDoc comment = commentOpt.get();
            comment.setLikes(comment.getLikes() + 1);
            comment.setUpdateTime(LocalDateTime.now());

            commentDocRepository.save(comment);
            return Result.saveSuccess("点赞成功");
        } catch (Exception e) {
            return Result.error("点赞失败：" + e.getMessage());
        }
    }

    @Override
    public Result getCommentsByMovieId(Integer movieId, int page, int size) {
        try {
            // 分页参数
            Pageable pageable = PageRequest.of(page, size, Sort.by(Sort.Direction.DESC, "createTime"));

            // 查询一级评论
            Page<CommentDoc> commentsPage = commentDocRepository.findByMovieIdAndParentIdIsNull(
                    movieId, pageable);

            // 为每个一级评论加载其直接子评论
            commentsPage.forEach(comment -> {
                List<CommentDoc> children = commentDocRepository.findByParentId(comment.getId());
                comment.setChildren(children);
            });

            return Result.getSuccess(commentsPage);
        } catch (Exception e) {
            return Result.error("获取评论失败：" + e.getMessage());
        }
    }
}

